package com.lucy.disk.onlyoffice.controller;

import com.lucy.common.domain.ResponseDTO;
import com.lucy.disk.constant.FileTypeEnum;
import com.lucy.disk.onlyoffice.constant.DocumentStatus;
import com.lucy.disk.onlyoffice.entity.Document;
import com.lucy.disk.onlyoffice.entity.DocumentEditCallback;
import com.lucy.disk.onlyoffice.entity.DocumentEditCallbackResponse;
import com.lucy.disk.onlyoffice.entity.DocumentEditParam;
import com.lucy.disk.onlyoffice.service.DocumentService;
import com.lucy.disk.utils.DiskUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("onlyoffice")
@Api(tags = "OnlyOffice服务")
public class OnlyOfficeController {
    @Autowired
    DocumentService documentService;



    @ApiIgnore
    @PostMapping("callback")
    @ApiOperation(value = "修改回调",notes = "")
    public DocumentEditCallbackResponse callBack(@RequestBody DocumentEditCallback callback) throws IOException {
        if (log.isInfoEnabled()) {
            log.info("### 编辑后回调, 回调信息：{}", callback);
        }
        // 需要保存时写出文件
        if (callback.getStatus() == DocumentStatus.READY_FOR_SAVING.getCode() || callback.getStatus() == DocumentStatus.BEING_EDITED_STATE_SAVED.getCode()) {
            if (log.isDebugEnabled()) {
                log.debug("@@@ 开始保存文件。。。");
            }
            boolean success = documentService.saveDocumentFile(callback.getKey(), callback.getUrl());
            if (log.isDebugEnabled()) {
                log.debug("@@@ 保存文件结束！");
            }
            if (!success) {
                return DocumentEditCallbackResponse.failue();
            }
        }
        return DocumentEditCallbackResponse.success();
    }
}
